package com.example.nbshopping.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.nbshopping.R;
import com.example.nbshopping.login.LoginFragment;
import com.example.nbshopping.login.LoginVPAdpter;
import com.example.nbshopping.login.RegisterFragment;

import java.util.ArrayList;
import java.util.List;

public class LoginRegActivity extends AppCompatActivity {
    TextView logintv,regtv;
    ViewPager loginvp;
    List<Fragment> fragmentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_reg);
        //接收上一个页面传来的数据  0--登录   1--注册
        int flag=getIntent().getIntExtra("flag",0);
        initView();//初始化部件
        setListener();
        initPager();//声明viewpager的展示页
        setPagerPos(flag);
    }
    //设置显示位置
    @SuppressLint("ResourceAsColor")
    private void setPagerPos(int pos) {
        if(pos==0){
            logintv.setTextColor(Color.rgb(241,104,173));
            regtv.setTextColor(Color.WHITE);
        }
        else if(pos==1){
            logintv.setTextColor(Color.WHITE);
            regtv.setTextColor(Color.rgb(241,104,173));
        }
        loginvp.setCurrentItem(pos);
    }

    private void initPager() {
        fragmentList=new ArrayList<>();
        fragmentList.add(new LoginFragment());
        fragmentList.add(new RegisterFragment());
        LoginVPAdpter vpAdpter=new LoginVPAdpter(getSupportFragmentManager(),fragmentList);
        loginvp.setAdapter(vpAdpter);//设置适配器
    }

    //设置监听事件
    private void setListener() {
        logintv.setOnClickListener(onClickListener);
        regtv.setOnClickListener(onClickListener);
        loginvp.addOnPageChangeListener(listener);
    }
    ViewPager.OnPageChangeListener listener=new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            setPagerPos(position);
        }
    };
    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id){
                case R.id.login_tv_log:
                    setPagerPos(0);
                    break;
                case R.id.login_tv_reg:
                    setPagerPos(1);
                    break;
            }
        }
    };
    private void initView() {
        logintv=findViewById(R.id.login_tv_log);
        regtv=findViewById(R.id.login_tv_reg);
        loginvp=findViewById(R.id.login_vp);
    }
}